<template>
    <div>
        <div class="head">
            <el-form inline label-width="120px" size="small">

                <el-form-item>
                    <el-input placeholder="请输入部门ID" v-model="searchData.id" @keyup.enter.native="search"
                              style="width: 200px">
                    </el-input>
                </el-form-item>
                <el-form-item>
                    <el-input placeholder="请输入部门名称" v-model="searchData.deptName" @keyup.enter.native="search"
                              style="width: 200px">
                    </el-input>
                </el-form-item>


                <el-form-item>
                    <el-button icon="el-icon-delete" type="danger" @click="clearForm">重置</el-button>
                    <el-button plain type="primary" icon="el-icon-search" @click="search">搜索</el-button>
                </el-form-item>
            </el-form>
            <div>
                <!-- 新增 -->
                <el-button size="small" plain type="primary" icon="el-icon-plus" @click="openAddDialog">新增<i
                        class="el-icon--right"></i>
                </el-button>
            </div>

        </div>
        <div class="table-menu">
            <el-table
                    :data="treeData"
                    style="width: 100%;margin-bottom: 20px;"
                    row-key="id"
                    border
                    ref="refTable"
                    v-loading="loading"
                    default-expand-all=""
                    :tree-props="{children: 'children', hasChildren: 'hasChildren'} ">
                <el-table-column
                        prop="id"
                        label="部门ID" width="100px">
                    <template slot-scope="scope">
                        <span>{{ scope.row.id }}</span>
                    </template>
                </el-table-column>
                <el-table-column
                        prop="parentId"
                        label="父级ID" align="center" width="100px">
                    <template slot-scope="scope">
                        <span>{{ scope.row.parentId }}</span>
                    </template>
                </el-table-column>

                <el-table-column
                        prop="name"
                        label="部门名称"
                        align="center">
                    <template slot-scope="scope">
                        <span>{{ scope.row.name | stringTrim }}</span>
                    </template>
                </el-table-column>

                <el-table-column
                        prop="leader"
                        label="部门负责人"
                        align="center">
                    <template slot-scope="scope">
                        <span>{{ scope.row.leader | stringTrim }}</span>
                    </template>
                </el-table-column>

                <el-table-column
                        prop="mobile"
                        label="联系电话"
                        align="center">
                    <template slot-scope="scope">
                        <span>{{ scope.row.mobile | stringTrim }}</span>
                    </template>
                </el-table-column>
                <el-table-column
                        prop="email"
                        label="邮箱"
                        align="center">
                    <template slot-scope="scope">
                        <span>{{ scope.row.email | stringTrim }}</span>
                    </template>
                </el-table-column>


                <el-table-column
                        prop="status"
                        label="状态"
                        align="center">
                    <template slot-scope="scope">
                        <span v-if="scope.row.status===0">正常</span>
                        <span v-if="scope.row.status===1">禁用</span>
                    </template>
                </el-table-column>


                <el-table-column label="创建时间" align="center">
                    <template slot-scope="scope">
                        <span>{{ scope.row.createTime }}</span>
                    </template>
                </el-table-column>


                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button plain size="mini" type="success"
                                   @click="openAddDialog(scope.row)">新增
                        </el-button>
                        <el-button plain size="mini" type="warning"
                                   @click="openUpdateDialog(scope.row)">编辑
                        </el-button>
                        <el-button plain size="mini"
                                   type="danger" @click="del(scope.row.id)">
                            删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!-- 添加或修改部门对话框 -->
            <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body center>
                <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                    <el-row>
                        <el-col :span="24">
                            <el-form-item label="上级部门" v-if="choiceNode.name !==undefined" id="top_dept">
                                <el-input v-model="choiceNode.name" disabled/>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="部门名称" prop="deptName">
                                <el-input v-model="form.name" placeholder="请输入部门名称"/>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="显示排序" prop="orderNum">
                                <el-input-number v-model="form.sort" controls-position="right" :min="0"/>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="负责人" prop="leader">
                                <el-input v-model="form.leader" placeholder="请输入负责人" maxlength="20"/>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="联系电话" prop="phone">
                                <el-input v-model="form.mobile" placeholder="请输入联系电话" maxlength="11"/>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="邮箱" prop="email">
                                <el-input v-model="form.email" placeholder="请输入邮箱" maxlength="50"/>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="部门状态" prop="locked">
                                <el-radio v-model="form.status" :label="0">正常</el-radio>
                                <el-radio v-model="form.status" :label="1">禁用</el-radio>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="cancel">取 消</el-button>
                    <el-button type="primary" @click="submitForm">确 定</el-button>
                </div>
            </el-dialog>

        </div>
    </div>
</template>

<script>
    import {dept_list, dept_del, dept_update, dept_detail, dept_add} from "@/api/sysdept";
    import {operate_log_add} from "@/api/sysconfig"

    export default {
        name: "dept-list",
        data() {
            return {
                treeData: [],
                defaultProps: {
                    children: "children",
                    label: "title"
                },
                choiceNode: {
                    id: 0,
                    name: "-"
                },
                // 部门树选项
                deptOptions: [],
                // 状态数据字典
                statusOptions: [],
                addDialogVisible: false,
                form: {
                    parentId: ""
                },
                searchData: {
                    deptName: "",
                    id: ""
                },
                rules: {
                    path: [{required: true, message: "请输入路由", trigger: "blur"}],
                    title: [{required: true, message: "请输入名称", trigger: "blur"}],
                    sort: [
                        {
                            required: true,
                            type: "number",
                            message: "请输入排序",
                            trigger: "blur"
                        }
                    ]
                },
                loading: false,
                expand: false,
                title: "新增部门",
                open: false
            };
        },
        mounted() {
            this.getData();

        },
        methods: {

            del(id) {
                this.$confirm("是否删除该部门?", "操作提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                }).then(() => {
                    dept_del(id).then(res => {
                        if (res.status === 200) {
                            this.getData();
                            this.$message.success("删除成功");
                            this.recordLog("部门-删除")
                        }
                    });
                });
            },

            search() {
                this.getData();
            },
            //日志记录
            recordLog(content) {
                let params = {};
                var list = localStorage.getItem('user');
                var lists = JSON.parse(list);
                params.sysUserId = lists.userId;
                params.content = content;
                operate_log_add(params).then()
            },
            // 取消按钮
            cancel() {
                this.open = false;
                this.form = {};
                this.choiceNode = {}
            },
            /** 提交按钮 */
            submitForm: function () {
                console.log(this.form.name);
                this.$refs["form"].validate(valid => {
                    if (valid) {
                        if (this.form.name !== undefined) {
                            dept_update(this.form).then(res => {
                                if (res.status === 200) {
                                    this.$message.success("修改成功");
                                    this.open = false;
                                    this.getData();
                                    this.recordLog("部门-修改")
                                }
                            });
                        } else {
                            this.form.parentId = this.choiceNode.id;
                            dept_add(this.form).then(res => {
                                if (res.status === 200) {
                                    this.$message.success("新增成功");
                                    this.open = false;
                                    this.form = {};
                                    this.getData();
                                    this.recordLog("部门-添加")
                                }
                            });
                        }
                    }
                });
            },
            getData() {
                this.loading = true;
                let params = {};
                params.id = this.searchData.id;
                params.deptName = this.searchData.deptName;
                dept_list(params).then(res => {
                    if (res.status === 200) {
                        this.treeData = res.data;
                        this.loading = false;
                    }
                });
            },
            clearForm() {
                this.searchData = {};
                this.getData();
            },
            //打开新增弹窗
            openAddDialog(row) {
                if (row.name === undefined) {
                    //添加顶层部门
                    this.open = true;
                    this.choiceNode.id = 0;
                    let top_dept = document.getElementById("top_dept");
                    top_dept.style.display = "none";
                } else {
                    console.log(row);
                    //添加子部门
                    this.choiceNode.name = row.name;
                    this.open = true;
                    this.choiceNode.id = row.id;
                }

            },
            //打开编辑弹窗
            openUpdateDialog(row) {
                this.title = "编辑弹窗";
                this.open = true;
                this.choiceNode.name = row.name;
                dept_detail(row.id).then(res => {
                    if (res.status === 200) {
                        this.form = res.data;
                    }
                })
            }
        }
    }
</script>

<style scoped>

</style>
